#coding=utf-8

import os

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True
    FLASK_MAIL_SUBJECT_PREFIX = '[FLASKY]'
    FLASK_MAIL_SENDER = 'Flasky Admin <cjd7749@qq.com>'
    FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN') or 'cjd9023@qq.com'
    FLASKY_POSTS_PER_PAGE = 15
    FLASKY_FOLLOWERS_PER_PAGE = 10
    FLASKY_COMMENTS_PER_PAGE = 10
    FLASK_POSTS_PER_PAGE = 10
    FLASKY_SLOW_DB_QUERY_TIME = 2
    SQLALCHEMY_RECORD_QUERIES = True
    FLASKY_DB_QUERY_TIMEOUT = 2

    @staticmethod
    def init_app(app):
        pass

class DevelopmentConfig(Config):
    DEBUG = True
    MAIL_SERVER = 'smtp.qq.com'
    MAIL_PORT = 465
    MAIL_USE_SSL = True
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME') or '3100865876'
    MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') or 'jbelehqmjlxydcdd'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
          'mysql://root:sf123456@127.0.0.1/flaskblog?charset=utf8'
    FLASK_MAIL_SUBJECT_PREFIX = '[Flasky]'
    SQLALCHEMY_TRACK_MODIFICATIONS = True

class TestingConfig(Config):
    # for test
    WTF_CSRF_ENABLED = False
    #
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
                              'mysql://root:sf123456@127.0.0.1/flaskblog_test?charset=utf8'

class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
                              'mysql://root:sf123456@127.0.0.1/flaskblog?charset=utf8'
    MAIL_SERVER = 'smtp.qq.com'
    MAIL_PORT = 465
    MAIL_USE_SSL = True
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME') or '3100865876'
    MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') or 'jbelehqmjlxydcdd'
    FLASK_MAIL_SUBJECT_PREFIX = '[Flasky]'

    @classmethod
    def init_app(cls, app):
        Config.init_app(app)

        # send error info to admin by email
        import logging
        from logging.handlers import SMTPHandler
        credentials = None
        secure = None
        if getattr(cls, 'MAIL_USERNAME', None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            #if getattr(cls, 'MAIL_USE_TLS', None):
            if getattr(cls, 'MAIL_USE_SSL', None):
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
                fromaddr=cls.FLASKY_MAIL_SENDER,
                toaddrs=[cls.FLASKY_ADMIN],
                subject=cls.FLASKY_MAIL_SUBJECT_PREFIX+ ' Application Error.',
                credentials=credentials,
                secure=secure
            )
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

class UnixConfig(ProductionConfig):
    @classmethod
    def init_app(cls, app):
        ProductionConfig.init_app(app)

        #
        import logging
        from logging.handlers import SysLogHandler
        syslog_handler = SysLogHandler()
        syslog_handler.setLevel(logging.WARNING)
        app.logger.addHandler(syslog_handler)


config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'default': DevelopmentConfig
}
